package test.top1.com.atguigu.app.dws;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.common.functions.RichFlatMapFunction;
import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.api.common.state.StateTtlConfig;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.common.time.Time;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.AsyncDataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.WindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;
import test.top1.com.atguigu.app.fun.DimAsyncFunction;
import test.top1.com.atguigu.bean.TradeProvinceOrderWindow_01;
import test.top1.com.atguigu.utils.MyClickHouseUtil;
import test.top1.com.atguigu.utils.MyKafkaUtil;
import test.top1.com.atguigu.utils.WindowFunctionUtil;

import java.math.BigDecimal;
import java.time.Duration;
import java.util.concurrent.TimeUnit;

/**
 * ClassName: Dws10_TradeProvinceOrderWindow_01
 * Package: test.top1.com.atguigu.app.dws
 * Description:
 *
 * @Author ChenJun(有志男青年)
 * @Create 2023/5/12 18:07
 * @Version 1.0
 */
public class Dws10_TradeProvinceOrderWindow_01 {
    public static void main(String[] args) throws Exception {

        //TODO 1.获取运行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        // TODO 配置环境信息生产环境得有

//         env.enableCheckpointing(3000L, CheckpointingMode.EXACTLY_ONCE);
//        env.getCheckpointConfig().setCheckpointTimeout(60 * 1000L);
//        env.getCheckpointConfig().setMinPauseBetweenCheckpoints(3000L);
//        env.getCheckpointConfig().enableExternalizedCheckpoints(
//                CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION
//        );
//        env.setRestartStrategy(RestartStrategies.failureRateRestart(
//                10, Time.of(1L, TimeUnit.DAYS), Time.of(3L, TimeUnit.MINUTES)
//        ));
//        env.setStateBackend(new HashMapStateBackend());
//        env.getCheckpointConfig().setCheckpointStorage("hdfs://hadoop102:8020/gmall/ck");
//        System.setProperty("HADOOP_USER_NAME", "atguigu");

        //TODO 2.从kafka dwd 层读取 dwd_trade_order_detail 主题数据
        DataStreamSource<String> kafkaDS = env.fromSource(MyKafkaUtil.getKafkaSource("dwd_trade_order_detail", "Dws10_TradeProvinceOrderWindow_01"), WatermarkStrategy.noWatermarks(), "kafka-source");

        //TODO 3.过滤null数据 转为json格式
        SingleOutputStreamOperator<JSONObject> jsonObjDS = kafkaDS.flatMap(new FlatMapFunction<String, JSONObject>() {
            @Override
            public void flatMap(String value, Collector<JSONObject> out) throws Exception {
                if (value != null) {
                    out.collect(JSONObject.parseObject(value));
                }
            }
        });

        //TODO 4.提取时间戳
        SingleOutputStreamOperator<JSONObject> jsonObjDSWithWMDS = jsonObjDS.assignTimestampsAndWatermarks(WatermarkStrategy.<JSONObject>forBoundedOutOfOrderness(Duration.ofSeconds(2)).withTimestampAssigner(new SerializableTimestampAssigner<JSONObject>() {
            @Override
            public long extractTimestamp(JSONObject element, long recordTimestamp) {
                return element.getLong("create_time");
            }
        }));

        //TODO 5.按照订单明细id分组去重由left join产生的重复数据,并转换为JavaBean对象
        SingleOutputStreamOperator<TradeProvinceOrderWindow_01> tradeProvinceDS = jsonObjDSWithWMDS.keyBy(json -> json.getString("id"))
                .flatMap(new RichFlatMapFunction<JSONObject, TradeProvinceOrderWindow_01>() {

                    private ValueState<String> valueState;

                    @Override
                    public void open(Configuration parameters) throws Exception {

                        StateTtlConfig ttlConfig = new StateTtlConfig.Builder(Time.seconds(5))
                                .setUpdateType(StateTtlConfig.UpdateType.OnReadAndWrite)
                                .build();

                        ValueStateDescriptor<String> stateDescriptor = new ValueStateDescriptor<>("map-state", String.class);

                        stateDescriptor.enableTimeToLive(ttlConfig);

                        valueState = getRuntimeContext().getState(stateDescriptor);
                    }

                    @Override
                    public void flatMap(JSONObject value, Collector<TradeProvinceOrderWindow_01> out) throws Exception {

                        //获取状态的值
                        String lastValue = valueState.value();

                        if (lastValue == null) {
                            valueState.update("1");

                            out.collect(TradeProvinceOrderWindow_01.builder()
                                    .provinceId(value.getString("province_id"))
                                    .orderAmount(new BigDecimal(value.getString("split_total_amount")))
                                    .orderId(value.getString("order_id"))
                                    .build());
                        }
                    }
                });

        //TODO 6.按照order_id分组 并统计订单数
        SingleOutputStreamOperator<TradeProvinceOrderWindow_01> tradOrderIdDS = tradeProvinceDS.keyBy(TradeProvinceOrderWindow_01::getOrderId).
                map(new RichMapFunction<TradeProvinceOrderWindow_01, TradeProvinceOrderWindow_01>() {

                    private ValueState<String> orderIdValueState;

                    @Override
                    public void open(Configuration parameters) throws Exception {

                        StateTtlConfig ttlConfig = new StateTtlConfig.Builder(Time.seconds(5))
                                .setUpdateType(StateTtlConfig.UpdateType.OnReadAndWrite)
                                .build();

                        ValueStateDescriptor<String> stateDescriptor = new ValueStateDescriptor<>("order-state", String.class);
                        stateDescriptor.enableTimeToLive(ttlConfig);

                        orderIdValueState = getRuntimeContext().getState(stateDescriptor);
                    }

                    @Override
                    public TradeProvinceOrderWindow_01 map(TradeProvinceOrderWindow_01 value) throws Exception {

                        //获取状态值
                        String state = orderIdValueState.value();

                        if (state == null) {
                            orderIdValueState.update("1");
                            value.setOrderCount(1L);
                        } else {
                            value.setOrderCount(0L);
                        }

                        return value;
                    }
                });

        //TODO 7.按照省份id分组  开窗  聚合
        SingleOutputStreamOperator<TradeProvinceOrderWindow_01>  reduceDS =
                tradOrderIdDS.keyBy(TradeProvinceOrderWindow_01::getProvinceId)
                .window(TumblingEventTimeWindows.of(org.apache.flink.streaming.api.windowing.time.Time.seconds(10))).reduce(new ReduceFunction<TradeProvinceOrderWindow_01>() {
                    @Override
                    public TradeProvinceOrderWindow_01 reduce(TradeProvinceOrderWindow_01 value1, TradeProvinceOrderWindow_01 value2) throws Exception {
                        value1.setOrderAmount(value1.getOrderAmount().add(value2.getOrderAmount()));
                        value1.setOrderCount(value1.getOrderCount() + value2.getOrderCount());
                        return value1;
                    }
                }, new WindowFunction<TradeProvinceOrderWindow_01, TradeProvinceOrderWindow_01, String, TimeWindow>() {
                    @Override
                    public void apply(String s, TimeWindow window, Iterable<TradeProvinceOrderWindow_01> input, Collector<TradeProvinceOrderWindow_01> out) throws Exception {

                        WindowFunctionUtil.set(input, window, out);

                    }
                });

        reduceDS.print("reduceDS--->");

        //TODO 8.关联维度
        SingleOutputStreamOperator<TradeProvinceOrderWindow_01> provinceOrderDS =
                AsyncDataStream.unorderedWait(reduceDS, new DimAsyncFunction<TradeProvinceOrderWindow_01>("DIM_BASE_PROVINCE") {
            @Override
            public String getKey(TradeProvinceOrderWindow_01 input) {
                return input.getProvinceId();
            }

            @Override
            public void join(TradeProvinceOrderWindow_01 input, JSONObject dimInfo) {

                input.setProvinceName(dimInfo.getString("NAME"));
            }
        }, 60, TimeUnit.SECONDS);

        //TODO 9.写出
        provinceOrderDS.print("provinceOrderDS-->");
        provinceOrderDS.addSink(MyClickHouseUtil.getSinkFunction("insert into dws_trade_province_order_window values(?,?,?,?,?,?,?)"));

        //TODO 10.执行
        env.execute("Dws10_TradeProvinceOrderWindow_01");
    }
}
